<template>
  <mapgis-ui-feature-edit
    @feature-edit-change="featureEditChange"
    @get-renderer="getRenderer"
    :fieldInfo="fieldInfo"
    :featureType="featureType"
    :layerFeatureStyle="layerFeatureStyle"
    :rendererType="rendererType"
    :featureStyle="featureStyle"
  ></mapgis-ui-feature-edit>
</template>

<script>
import featureEditUtil from '../../mixin/feature-eidt-util'
import { LayerFeatureEdit } from '@mapgis/web-app-framework'

export default {
  name: 'MpFeatureEdit',
  mixins: [featureEditUtil],
  props: {
    layer: {
      type: Object,
    },
    fieldInfo: {
      type: Array,
      deafult: () => [],
    },
  },
  data() {
    return {
      layerFeatureEdit: LayerFeatureEdit,
    }
  },
  computed: {
    // 图层类型，点、线、区
    featureType() {
      // 从LayerFeatureEdit获取已存在的编辑样式
      const layerInfo = this.getFeatureStyle()
      // 如果LayerFeatureEdit中不存在需要从this.layer中获取
      if (!layerInfo) {
        const geomType = this.getFeatureStyleByLayer()
        return geomType
      }
      return layerInfo.type
    },
    // 图层要素数据
    layerFeatureStyle() {
      const renderer = this.getFeatureStyle()?.renderer
      if (renderer) {
        if (renderer.type == 'simple') {
          return renderer.symbol || {}
        } else if (renderer.type == 'unique-value') {
          return renderer.uniqueValueInfos.map((item) => {
            return {
              sample: item.symbol,
              class: item.value,
            }
          })
        } else if (renderer.type == 'class-breaks') {
          return renderer.classBreakInfos.map((item) => {
            return {
              sample: item.symbol,
              max: item.maxValue,
              min: item.minValue,
            }
          })
        }
      }
      return {}
    },
    // 图层专题图类型
    rendererType() {
      const renderer = this.getFeatureStyle()?.renderer
      return renderer?.type
    },
    // 图层专题图symbol类型
    featureStyle() {
      const symbolType = this.getFeatureStyle()?.symbolType
      return symbolType
    },
  },
  mounted() {
    window.getRenderer = () => this.getRenderer()
  },

  methods: {
    getFeatureStyleByLayer() {
      const { geomType, featureStyle } = this.layer
      // geojson数据有geomType为空的情况
      if (!geomType && featureStyle) {
        const transformType = this.getGeometryType(featureStyle.type)
        return transformType
      }
      return geomType
    },
    getGeometryType(type) {
      let transformType
      switch (type) {
        case 'point':
          transformType = 'Pnt'
          break
        case 'line':
          transformType = 'Lin'
          break
        case 'polygon':
          transformType = 'Reg'
          break
        default:
          break
      }
      return transformType
    },
    getFeatureStyle() {
      const { layer } = this.layer
      let layerInfo
      if (layer) {
        layerInfo = this.layerFeatureEdit.getFeatureRelation(
          layer.url,
          this.layer.url
        )
      } else {
        layerInfo = this.layerFeatureEdit.getFeatureRelation(this.layer.url)
      }
      return layerInfo
    },
    // feature-edit-change
    featureEditChange(type, params, callback) {
      const gdbpUrl = this.isGdbpType(this.layer.url)
      if (gdbpUrl) {
        this.featureChangeByGdbp(type, params, callback)
      } else {
        this.featureChangeByUrl(type, params, callback)
      }
    },
    async featureChangeByGdbp(type, params, callback) {
      const { statisticsField, groupField } = params
      const maxStatisticsType = {
        label: '最大值',
        value: 'FUNCTION_MAX',
        type: 'max',
      }
      const minStatisticsType = {
        label: '最小值',
        value: 'FUNCTION_MIN',
        type: 'min',
      }
      let result
      switch (type) {
        // 一值一类
        case 'uniqueValue':
          const uniqueValueData = await this.getStatisticsResult(
            this.layer,
            'single',
            statisticsField,
            groupField,
            maxStatisticsType
          )
          result = this.transformStatisticsData('uniqueValue', uniqueValueData)
          break
        // 分类
        case 'classBreak':
          const minData = await this.getStatisticsResult(
            this.layer,
            'single',
            statisticsField,
            groupField,
            minStatisticsType
          )
          const maxData = await this.getStatisticsResult(
            this.layer,
            'single',
            statisticsField,
            groupField,
            maxStatisticsType
          )
          result = [
            ...this.transformStatisticsData('classBreakMin', minData),
            ...this.transformStatisticsData('classBreakMax', maxData),
          ]
          break
        default:
          break
      }
      callback(result)
    },
    featureChangeByUrl(type, params, callback) {
      // 从geojson数据中过滤一值一类只需要分组字段即可，统计字段不使用,分段则反之
      const { statisticsField, groupField } = params
      const {
        source: { features },
      } = this.layer
      let result
      switch (type) {
        case 'uniqueValue':
          result = this.filterFeatureSet(type, groupField.value, features)
          break
        case 'classBreak':
          result = this.filterFeatureSet(
            type,
            statisticsField[0].value,
            features
          )
          break
        default:
          break
      }
      callback(result)
    },
    getRenderer(renderer = {}, symbolType, isSave) {
      const { layer } = this.layer
      let extend
      const isEdit = Object.keys(renderer).length > 0
      // 记录设置信息到layerFeatureEdit中
      if (layer) {
        const hasFeatureStyle = this.layerFeatureEdit.getFeatureRelation(
          layer.url,
          this.layer.url
        )
        if (hasFeatureStyle) {
          if (isEdit) {
            this.layerFeatureEdit.updateFeatureRenderer(
              layer.url,
              this.layer.url,
              renderer,
              symbolType
            )
          } else {
            this.layerFeatureEdit.deleteFeatureRelation(
              layer.url,
              this.layer.url
            )
          }
        } else {
          if (isEdit) {
            const relation = this.getFeatureRelation(
              this.layer,
              symbolType,
              renderer,
              true
            )
            this.layerFeatureEdit.setFeatureRelation(layer.url, relation)
          }
        }

        extend = layer.extend
      } else {
        const hasFeatureStyle = this.layerFeatureEdit.getFeatureRelation(
          this.layer.url
        )
        if (hasFeatureStyle) {
          if (isEdit) {
            this.layerFeatureEdit.updateFeatureRenderer(
              this.layer.url,
              undefined,
              renderer,
              symbolType
            )
          } else {
            this.layerFeatureEdit.deleteFeatureRelation(this.layer.url)
          }
        } else {
          if (isEdit) {
            const relation = this.getFeatureRelation(
              this.layer,
              symbolType,
              renderer,
              false
            )
            this.layerFeatureEdit.setFeatureRelation(this.layer.url, relation)
          }
        }

        extend = this.layer.extend
      }
      // 在extend上记录对应子图层的编辑样式
      this.changeFeatureStyle(extend, renderer, symbolType, isEdit)
      this.$emit('update:layer', this.layer, isSave)
    },
    changeFeatureStyle(extend, renderer, symbolType, isEdit) {
      if (isEdit) {
        if (!extend.featureStyle) {
          extend.featureStyle = {}
        }
        if (!extend.featureStyle[this.layer.url]) {
          extend.featureStyle[this.layer.url] = {}
        }
        extend.featureStyle[this.layer.url].renderer = renderer
        extend.featureStyle[this.layer.url].symbolType = symbolType
        extend.featureStyle[this.layer.url].type = this.featureType
      } else {
        if (extend.featureStyle && extend.featureStyle[this.layer.url]) {
          delete extend.featureStyle[this.layer.url]
        }
        // 如果extend.featureStyle为空则直接删除
        if (
          extend.featureStyle &&
          Object.keys(extend.featureStyle).length === 0
        ) {
          delete extend.featureStyle
        }
      }
    },
    getFeatureRelation(layer, symbolType, renderer, hasParent) {
      let relation = {}
      const sublayer = {
        id: layer.id,
        type: this.featureType,
        symbolType: symbolType,
        renderer: renderer,
      }
      if (hasParent) {
        relation[layer.url] = sublayer
      } else {
        relation = sublayer
      }
      return relation
    },
  },
}
</script>

<style></style>
